package com.lninl.service;

import com.lninl.model.Uri;
import com.lninl.model.User;
import com.lninl.model.UserUri;
import com.lninl.repository.UserUriRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.Transient;

/**
 * Mostly used as a facade for all Markme controllers Also a placeholder for @Transactional
 * and @Cacheable annotations
 *
 * @author qinains
 */
@Service
public class UserUriServiceImpl implements UserUriService {
    @Autowired
    private UserUriRepository userUriRepository;

    @Override
    @Transient
    public UserUri save(User user, Uri uri) {
        UserUri userUri = userUriRepository.findByUserIdAndUriId(user.getId(), uri.getId());
        if (userUri == null) {
            userUri.setUser(user);
            userUri.setUri(uri);
            return userUriRepository.save(userUri);
        }
        return userUri;
    }
}
